package com.bookmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookmall.common.constant.MyInfoTypeConstants;
import com.bookmall.common.utils.MyQuery;
import com.bookmall.common.utils.PageUtils;
import com.bookmall.product.domain.BookCategory;
import com.bookmall.product.domain.BookInfoImages;
import com.bookmall.product.domain.vo.BookBuyVo;
import com.bookmall.product.mapper.BookCategoryMapper;
import com.bookmall.product.mapper.BookInfoImagesMapper;
import com.bookmall.product.service.IBookInfoImagesService;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bookmall.product.mapper.BookBuyMapper;
import com.bookmall.product.domain.BookBuy;
import com.bookmall.product.service.IBookBuyService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 求购信息Service业务层处理
 * 
 * @author 胡菜鸡
 * @date 2022-03-13
 */
@Service
public class BookBuyServiceImpl extends ServiceImpl<BookBuyMapper, BookBuy> implements IBookBuyService {

    @Autowired
    private BookBuyMapper bookBuyMapper;

    @Autowired
    private BookCategoryMapper categoryMapper;

    @Autowired
    private IBookInfoImagesService bookImagesService;

    @Autowired
    private BookInfoImagesMapper bookImagesMapper;

    /**
     * 按条件分页查询分页信息
     * @author 胡菜鸡
     * @date 2022/3/13 18:32
     */
    @Override
    public PageUtils selectBookBuyList(BookBuy bookBuy) {
        LambdaQueryWrapper<BookBuy> wrapper = new LambdaQueryWrapper<>(bookBuy);
        IPage<BookBuy> page = this.page(new MyQuery<BookBuy>().getPage(bookBuy), wrapper);
        return new PageUtils(page);
    }

    /**
     * 修改书籍求购状态
     * @author 胡菜鸡
     * @date 2022/3/14 21:16
     */
    @Override
    public int updateBuyStatus(BookBuy bookBuy) {
        BookBuy newBookBuy = new BookBuy();
        newBookBuy.setId(bookBuy.getId());
        newBookBuy.setStatus(bookBuy.getStatus());
        return bookBuyMapper.updateById(newBookBuy);
    }

    /**
     * 插入书籍求购信息
     * @author 胡菜鸡
     * @date 2022/3/14 21:24
     */
    @Override
    @Transactional
    public int insertBookBuy(BookBuyVo bookBuyVo) {
        /*
            插入书籍信息
         */
        BookBuy bookBuy = new BookBuy();
        BeanUtils.copyProperties(bookBuyVo, bookBuy);
        bookBuy.setCreateBy(SecurityUtils.getUsername());
        bookBuy.setCreateTime(DateUtils.getNowDate());
        //设置书籍类别名
        BookBuy bookBuyResult = this.setBookTypeName(bookBuy);
        int i = bookBuyMapper.insert(bookBuyResult);

        /*
            插入书籍图片
         */
        int j = 1;
        String imagesUrl = bookBuyVo.getImageUrl();
        if(!StringUtils.isBlank(imagesUrl)){
            j = bookImagesService.insertImagesWithUrl(imagesUrl, bookBuy.getId(), MyInfoTypeConstants.BUY_IMAGE);
        }
        return i & j;
    }

    /**
     * 修改书籍求购信息
     * @author 胡菜鸡
     * @date 2022/3/14 21:28
     */
    @Override
    @Transactional
    public int updateBookBuy(BookBuyVo bookBuyVo) {
        /*
            修改书籍信息
         */
        BookBuy bookBuy = new BookBuy();
        BeanUtils.copyProperties(bookBuyVo, bookBuy);
        bookBuy.setUpdateBy(SecurityUtils.getUsername());
        bookBuy.setUpdateTime(DateUtils.getNowDate());
        //设置书籍类别名称
        BookBuy bookBuyResult = this.setBookTypeName(bookBuy);
        int i = bookBuyMapper.updateById(bookBuyResult);

        /*
            修改书籍图片
         */
        int j = 1;
        String imagesUrl = bookBuyVo.getImageUrl();
        if(!StringUtils.isBlank(imagesUrl)) {
            //删除原来的图片
            LambdaQueryWrapper<BookInfoImages> wrapper = new LambdaQueryWrapper<>();
            bookImagesMapper.delete(wrapper.eq(BookInfoImages::getBookId, bookBuy.getId()));

            //插入新的图片
            j = bookImagesService.insertImagesWithUrl(imagesUrl, bookBuy.getId(), MyInfoTypeConstants.BUY_IMAGE);
        }
        return i & j;
    }

    /**
     * 通过id查询书籍求购的详细信息
     * @author 胡菜鸡
     * @date 2022/3/14 22:07
     */
    @Override
    public BookBuyVo selectBookBuyById(String id) {
        BookBuy bookBuy = bookBuyMapper.selectById(id);
        BookBuyVo bookBuyVo = new BookBuyVo();
        BeanUtils.copyProperties(bookBuy, bookBuyVo);
        String imagesUrl = bookImagesService.selectImageUrl(id);
        bookBuyVo.setImageUrl(imagesUrl);
        return bookBuyVo;
    }

    /**
     * 设置求购书籍分类名称
     * @author 胡菜鸡
     * @date 2022/3/12 16:26
     */
    private BookBuy setBookTypeName(BookBuy bookBuy){
        //书籍类型是否为空
        if(!StringUtils.isBlank(bookBuy.getBuyType())){
            //查询此书籍类型
            BookCategory bookCategory = categoryMapper.selectById(bookBuy.getBuyType());
            if(bookCategory != null) {
                // 书籍类型存在
                bookBuy.setBuyTypeName(bookCategory.getCatName());
                return bookBuy;
            }
        }
        // 不存在
        bookBuy.setBuyType(null);
        bookBuy.setBuyTypeName("无");
        return bookBuy;
    }

    /**
     * 根据id批量删除
     * @author 胡菜鸡
     * @date 2022/3/14 22:30
     */
    @Override
    @Transactional
    public int deleteBookBuyByIds(List<String> ids) {
        LambdaQueryWrapper<BookInfoImages> wrapper = new LambdaQueryWrapper<>();
        bookImagesMapper.delete(wrapper.in(BookInfoImages::getBookId, ids));
        return bookBuyMapper.deleteBatchIds(ids);
    }
}
